/**
 * 
 */
Ext.ns('Ext.lams');
/**
 * 学校信息面板
 * 
 * @class Ext.lams.schoolFormPanel
 * @extends Ext.form.FormPanel
 */
Ext.lams.schoolFormPanel = Ext.extend(Ext.form.FormPanel, {
			constructor : function(config) {
				Ext.apply(this, config || {});
				var _vtype = this.vt == null ? '' : this.vt;
				Ext.lams.schoolFormPanel.superclass.constructor.call(this, {
							layout : 'form',
							frame : true,
							// layout : {
							// type : 'form',
							// //align : 'stretch'
							// },
							defaults : {
								xtype : 'textfield',
								anchor : '95%',
								labelWidth : 65
							},
							// baseCls : "x-plain",
							items : [{
										xtype : 'hidden',
										name : 'SCHOOL_ID'
									}, {
										fieldLabel : '学校名称',
										allowBlank : false,
										vtype : _vtype,
										blankText : "该项为必填项",
										name : "SCHOOL_NAME"
									}, {
										xtype : 'treecombobox',
										fieldLabel : '所属单位',
										emptyText : '请选择...',
										treeUrl : 'getUnitTree.action',
										rootText : '加载节点',
										rootId : '0',
										hiddenName : "ORG_NO"
									}, {
										fieldLabel : '所在辖区',
										allowBlank : true,
										blankText : "所在市/区",
										name : "RESP_DIST"
									}, {
										xtype : 'combo_scode',
										fieldLabel : '学校等级',
										code_cate : 'SCHOOL_LEVEL',
										hiddenName : "SCHOOL_LEVEL"
									}, {
										xtype : 'combo_scode',
										fieldLabel : '学校类型',
										code_cate : 'SCHOOL_TYPE',
										hiddenName : "SCHOOL_TYPE"
									}, {
										fieldLabel : '联系人',
										allowBlank : true,
										blankText : "联系人",
										name : "CONTACTOR"
									}, {
										fieldLabel : '联系电话',
										allowBlank : true,
										blankText : "联系电话",
										name : "CONTACT_PHONE"
									}, {
										fieldLabel : '地址',
										allowBlank : true,
										blankText : "地址",
										name : "ADDRESS"
									}, {
										xtype : 'textarea',
										fieldLabel : '备注',
										name : 'REMARK'
									}]
						});
			},
			getValue : function() {
				if (this.getForm().isValid())
					return new Ext.data.Record(this.getForm().getValues());
				else
					throw Error("表单填写出错");
			},
			setValue : function(_r) {
				this.getForm().loadRecord(_r);
				var node = {
					id : _r.data['ORG_NO'],
					text : _r.data['ORG_NAME']
				}
				this.find('fieldLabel', '所属单位')[0].setValue(node);
			},
			clear : function() {
				this.getForm().reset();
			}
		})
/**
 * 学校信息窗口
 * 
 * @class Ext.lams.schoolInfoWindow
 * @extends Ext.Window
 */
Ext.lams.schoolInfoWindow = Ext.extend(Ext.Window, {
	form : null,
	constructor : function(config) {
		Ext.apply(this, config || {});
		var _vtype = this.vt == null ? '' : this.vt;
		this.form = new Ext.lams.schoolFormPanel({
					'vt' : _vtype
				});
		Ext.lams.schoolInfoWindow.superclass.constructor.call(this, {
					plain : true,
					width : 400,
					closeAction : "hide",
					items : this.form,
					modal : true,
					buttons : [{
								xtype : "btn_submit",
								handler : this.onSubmitClick,
								scope : this
							}, {
								xtype : "btn_cancel",
								handler : this.onCancelClick,
								scope : this
							}],
					listener : {

		}
				});
		this.addEvents("submit");
	},
	close : function() {
		this.form.clear();
		this.hide();
	},
	onSubmitClick : function() {
		var _win = this;
		try {
			this.form.form.submit({
						url : 'saveOrUpdateSchool.action',
						method : 'POST',
						waitMsg : '保存数据...',
						params : {
							'ORG_NO' : _win.form.find('hiddenName', 'ORG_NO')[0]
									.getValue()
						},
						success : function(form, action) {
							// Ext.Msg.alert('success',
							// action.result.message,_win.close());
							H.Msg.info(action.result.message, function() {
										_win.fireEvent("submit", this);
										_win.close()
									});
						},
						failure : function(form, action) {
							// Ext.Msg.alert('failure', action.result.message);
							H.Msg.error(action.result.message);
						}
					});
		} catch (e) {
			// Ext.Msg.alert("提示", e.message);
			H.Msg.error(e.message);
		}
	},
	onCancelClick : function() {
		this.close();
	}
})
/**
 * 新增学校窗口
 * 
 * @class AddSchoolWindow
 * @extends Ext.lams.schoolInfoWindow
 */
AddSchoolWindow = Ext.extend(Ext.lams.schoolInfoWindow, {
	title : "添加学校"
		// vt : 'checkSchoolInfo'
	})
/**
 * 编辑学校窗口
 * 
 * @class UpdateSchoolWindow
 * @extends Ext.lams.schoolInfoWindow
 */
UpdateSchoolWindow = Ext.extend(Ext.lams.schoolInfoWindow, {
			title : '修改学校',
			load : function(_r) {
				this.form.setValue(_r);
			}
		})
Ext.lams.SchoolManagerPanel = Ext.extend(Ext.grid.GridPanel, {
			addWin : new AddSchoolWindow(),
			updateWin : new UpdateSchoolWindow(),
			constructor : function(config) {
				Ext.apply(this, config || {});
				Ext.lams.SchoolManagerPanel.superclass.constructor.call(this, {
							frame : true,
							layout : 'form',
							loadMask : {
								msg : '正在载入数据,请稍等...'
							},
							listeners : {
								dblclick : function() {
									this.updateWin.show();
									try {
										this.updateWin.load(this
												.getSelectedItem());
									} catch (_err) {
										// Ext.Msg.alert("提示", _err.message);
										H.Msg.error(_err.message, function() {
													this.updateWin.close();
												});
									}
								}
							}
						});
			},
			initComponent : function() {
				var sm = new Ext.grid.RowSelectionModel({
							singleSelect : true
						});
				var cm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(),
						{
							header : "学校名称",
							width : 150,
							dataIndex : "SCHOOL_NAME"
						}, {
							header : '所属单位',
							width : 150,
							dataIndex : "ORG_NAME"
						}, {
							header : '所在辖区',
							width : 100,
							dataIndex : "RESP_DIST"
						}, {
							header : "学校等级",
							width : 100,
							dataIndex : "SCHOOL_LEVEL_NAME"
						}, {
							header : "学校类型",
							width : 100,
							dataIndex : "SCHOOL_TYPE_NAME"
						}, {
							header : "地址",
							width : 200,
							dataIndex : "ADDRESS"
						}]);
				var tbar = [{
							xtype : 'btn_add',
							// id : 'btn_add',
							// disabled : Utils.isEnableBtn(this.btns,
							// 'btn_add'),
							width : '75',
							handler : function() {
								this.addWin.show();
							},
							scope : this
						}, "-", {
							xtype : 'btn_edit',
							// disabled :
							// Utils.isEnableBtn(this.btns,'btn_modify'),
							width : '75',
							handler : function() {
								this.updateWin.show();
								try {
									this.updateWin.load(this.getSelectedItem());
								} catch (_err) {
									// Ext.Msg.alert("提示", _err.message);
									H.Msg.error(_err.message, function() {
												this.updateWin.close();
											});
								}
							},
							scope : this
						}, "-", {
							xtype : 'btn_delete',
							width : '75',
							handler : this.removeSchool,
							scope : this
						}, "-", '单位', {
							// id : "treecombo",
							xtype : 'treecombobox',
							emptyText : '请选择...',
							treeUrl : 'getUnitTree.action',
							rootText : '加载节点',
							rootId : '0'
						}, "-", {
							xtype : 'btn_query',
							// id : 'btn_query',
							width : '75',
							handler : function(btn) {
								btn.disable();
								_store.baseParams = {
									'ORG_NO' : this.getTopToolbar().items.items[7]
											.getValue()
								}
								_store.load({
											params : {
												start : 0,
												limit : bbar.pageSize
											},
											callback : function() {
												btn.enable();
											}
										});
							},
							scope : this
						}];
				var _store = new Ext.data.JsonStore({
							// autoLoad:true,
							proxy : new Ext.data.HttpProxy({
										url : "getSchools.action",
										method : "GET"
									}),
							totalProperty : "totalCount",
							root : "data",
							fields : ['SCHOOL_ID', 'SCHOOL_NAME', 'ORG_NO',
									'ORG_NAME', 'RESP_DIST',
									'SCHOOL_LEVEL_NAME', 'SCHOOL_LEVEL',
									'SCHOOL_TYPE_NAME', 'SCHOOL_TYPE',
									'ADDRESS', 'CONTACT_PHONE', 'CONTACTOR',
									'REMARK']
						});
				var bbar = new Ext.lams.comp.PagingToolbar({
							store : _store
						});
				Ext.apply(this, {
							store : _store,
							sm : sm,
							cm : cm,
							tbar : tbar,
							bbar : bbar
						});
				this.addEvents('rowselect');
				this.addWin.on("submit", this.onAddWinSubmit, this);
				this.updateWin.on("submit", this.onUpdateWinSubmit, this);
				Ext.lams.SchoolManagerPanel.superclass.initComponent.call(this);
			},
			getSelectedItem : function() {
				var _sel = this.getSelectionModel();
				if (_sel.getCount() == 0)
					throw new Error("未选中某条记录");
				return _sel.getSelected();
			},
			// addSchool : function(_r) {
			// this.getStore().add(_r);
			// },
			// modifySchool : function(_r) {
			// try {
			// var _sr = this.getSelectedItem();
			// var _data = _sr.data;
			// for (var _i in _data) {
			// _sr.set(_i, _r.get(_i));
			// }
			// _sr.commit();
			// } catch (_err) {
			// //Ext.Msg.alert("提示", _err.message);
			// H.Msg.error(_err.message);
			// }
			// },
			onAddWinSubmit : function(_win, _r) {
				this.getStore().reload();
				// this.addSchool(_r);
			},
			onUpdateWinSubmit : function(_win, _r) {
				this.getStore().reload();
				// this.modifySchool(_r);
			},
			removeSchool : function() {
				try {
					var sel = this.getSelectedItem();
					var grid = this;
					H.Msg.confirm("确定删除选中记录吗?", function(btn) {
								if (btn == 'yes') {
									Ext.Ajax.request({
												url : 'deleteSchool.action',
												method : 'POST',
												params : {
													'SCHOOL_ID' : sel.data['SCHOOL_ID']
												},
												success : function() {
													grid.getStore().remove(sel);
												},
												failure : function(action) {
													// Ext.Msg.alert('failure',
													// action.result.message);
													H.Msg
															.error(action.result.message);
												}
											})
								}
							});
				} catch (error) {
					// Ext.Msg.alert("提示", error.message);
					H.Msg.error(error.message);
				}
			}
		})